package com.fduss.mazegame.client.gui.fxgui;

import com.fduss.mazegame.CLOptions;
import com.fduss.mazegame.client.Localizer;
import com.fduss.mazegame.core.Update;
import com.fduss.mazegame.core.unit.feature.Icon;
import com.fduss.mazegame.server.Server;
import com.fduss.mazegame.util.Queue;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

import java.util.HashMap;
import java.util.Map;

/**
 * a java fx hui base on texture
 *
 * @Author duocai wu
 * @Date 2017/9/28
 * @Time 18:30
 */
public class TEXGui extends FxGui {

    private static final String IMAGE_BASE = "./img/";
    private Map<Icon, Image> imageMap = new HashMap<>();
    private ImageView[][] mapTiles;

    public static void main(String[] args) {
        opts = CLOptions.getInstance(args);
        // @TODO must use static server object at this time, address it later
        theme = opts.getTheme();
        launch();
    }

    private ImageView newImageView(Icon icon) {
        ImageView imageView = new ImageView(iconToImage(icon));
        imageView.setFitWidth(TILE_SIZE);
        imageView.setFitHeight(TILE_SIZE);
        return imageView;
    }

    private Image iconToImage(Icon icon) {
        Image image = imageMap.get(icon);
        if (image == null)
            throw new IllegalArgumentException(icon.toString());
        return image;
    }

    protected void initResources() {
        TILE_SIZE = 25;
        String path = IMAGE_BASE + theme.name() + "/";
        Image WALL_IMAGE = new Image(path + "wall.png");
        Image SPACE_IMAGE = new Image(path + "space.png");
        Image HERO_IMAGE = new Image(path + "hero.png");
        Image END_IMAGE = new Image(path + "end.png");
        Image FOOTPRINT_IMAGE = new Image(path + "footprint.png");
        Image MONSTER_IMAGE = new Image(path + "monster.png");
        Image TREASURE_IMAGE = new Image(path + "treasure.png");

        // add to image map
        imageMap.put(Icon.EMPTY, SPACE_IMAGE);
        imageMap.put(Icon.WALL, WALL_IMAGE);
        imageMap.put(Icon.HERO, HERO_IMAGE);
        imageMap.put(Icon.FOOTPRINT, FOOTPRINT_IMAGE);
        imageMap.put(Icon.MONSTER, MONSTER_IMAGE);
        imageMap.put(Icon.END, END_IMAGE);
        imageMap.put(Icon.TREASURE, TREASURE_IMAGE);
    }

    protected void updateMap(Icon[][] icons) {
        int numRows = icons.length;
        int numCols = icons[0].length;
        mapTiles = new ImageView[numRows][numCols];
        // north wall
        for (int c = 0; c < numCols + 2; c++) {
            map.add(newImageView(Icon.WALL), c, 0);
        }
        for (int r = 0; r < numRows; r++) {
            // west wall
            map.add(newImageView(Icon.WALL), 0, r + 1);
            // maze contents
            for (int c = 0; c < numCols; c++) {
                mapTiles[r][c] = newImageView(icons[r][c]);
                map.add(mapTiles[r][c], c + 1, r + 1);
            }
            // east wall
            map.add(newImageView(Icon.WALL), numCols + 1, r + 1);
        }
        // south wall
        for (int c = 0; c < numCols + 2; c++) {
            map.add(newImageView(Icon.WALL), c, numRows + 1);
        }
    }

    protected void showUpdates(Queue<Update> updates) {
        while (!updates.isEmpty()) {
            Update update = updates.dequeue();
            int row = update.getRow();
            int col = update.getCol();
            Icon icon = update.getIcon();
            ImageView imageView = mapTiles[row][col];
            imageView.setImage(iconToImage(icon));
        }
    }
}

